/*
 * 
 * @author Gregory Coleman & Cody Thompson
 * This class creates a Login panel and populates it with components.
 *
 */
package view;
//Greg is a super-cool gay!
import java.awt.Dimension;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;

import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JTextField;

import model.Conference;
import model.Database;
import model.User;

/**
 * The login panel.
 * @author Greg Coleman and Cody Thompson
 */
public class Login extends JPanel {

	/** Checks to see if this is a user. */
	private boolean exists = false;
	
	/** The list of conference. */
	private List<Conference> confs;
	
	/** The list of users. */
	private List<User> users;
	
	/** The size of the login screen. */
	private static Dimension LOGSIZE = new Dimension(470,300);
	
	/** A field for entering text. */
	private JTextField textField;
	
	/** A group of buttons to be displayed for the type of users. */
	private final ButtonGroup buttonGroup = new ButtonGroup();
	
	/** The radio buttons that let you choose the type of user. */
	private JRadioButton[] buttList =  new JRadioButton[4];
	
	/** The combo box. */
	private JComboBox<Conference> comboBox;
	
	/** The user. */
	private User me;
	
	/** The database. */
	private Database dbase;
	
	/** The main frame for the program. */
	private MainFrame mf;
	
	/** The level of access for the user. */
	private int accessLevel = 0;
	
	/** Changes the login button to a confirm button. */
	private int loginFlag = 0;
	
	/** The login button. */
	private JButton btnLogButton;

	/**
	 * Constructor for the login screen.
	 * @param DB The database.
	 * @param mainf The main frame.
	 */
	public Login(Database DB, MainFrame mainf) {
		super();
		mf = mainf; //pointer to the main frame
		setLayout(null);
		init();
		setPreferredSize(LOGSIZE);
		dbase = DB;
		loadUsers();
		loadConf();
		setupConfs();
	}

	/**
	 * Add components to panel.
	 */
	private void init() {
		textField = new JTextField();
		textField.setBounds(164, 63, 189, 20);
		add(textField);
		textField.setColumns(10);

		JLabel lblLoginLabel = new JLabel("Account Name:");
		lblLoginLabel.setBounds(58, 66, 110, 14);
		add(lblLoginLabel);

		comboBox = new JComboBox<Conference>();
		comboBox.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				clearSelected();
				if (exists){
					if (confs.get(comboBox.getSelectedIndex()).checkPC(me)) {
						buttList[2].setEnabled(true);
						accessLevel = 2;
					}
					if (confs.get(comboBox.getSelectedIndex()).checkSPC(me)) {
						buttList[3].setEnabled(true);
						accessLevel = 3;
					}
					if (confs.get(comboBox.getSelectedIndex()).checkReviewer(me)) {
						buttList[1].setEnabled(true);
						accessLevel = 1;
					}
					buttList[0].setEnabled(true);
				} else {

				}
			}
		});
		comboBox.setBounds(164, 94, 189, 20);
		add(comboBox);

		JLabel lblConLabel = new JLabel("Conference:");
		lblConLabel.setBounds(75, 97, 90, 14);
		add(lblConLabel);

		JLabel lblWelcomeLabel = new JLabel("Welcome Honored User!");
		lblWelcomeLabel.setFont(new Font("Vijaya", Font.BOLD, 24));
		lblWelcomeLabel.setBounds(70, 11, 350, 41);
		add(lblWelcomeLabel);
		btnLogButton = new JButton("Login");
		
		btnLogButton.setBounds(363, 62, 88, 23);
		add(btnLogButton);

		JRadioButton rdbtnAuthorRadioButton = new JRadioButton("Author");
		rdbtnAuthorRadioButton.setEnabled(false);
		buttonGroup.add(rdbtnAuthorRadioButton);
		rdbtnAuthorRadioButton.setBounds(84, 141, 109, 23);
		add(rdbtnAuthorRadioButton);

		JRadioButton rdbtnReviewerRadioButton_1 = new JRadioButton("Reviewer");
		rdbtnReviewerRadioButton_1.setEnabled(false);
		buttonGroup.add(rdbtnReviewerRadioButton_1);
		rdbtnReviewerRadioButton_1.setBounds(84, 181, 109, 23);
		add(rdbtnReviewerRadioButton_1);

		JRadioButton rdbtnPCRadioButton_2 = new JRadioButton("Program Chair");
		rdbtnPCRadioButton_2.setEnabled(false);
		buttonGroup.add(rdbtnPCRadioButton_2);
		rdbtnPCRadioButton_2.setBounds(244, 141, 109, 23);
		add(rdbtnPCRadioButton_2);

		JRadioButton rdbtnSPCRadioButton_3 = new JRadioButton("Sub-Program Chair");
		rdbtnSPCRadioButton_3.setEnabled(false);
		buttonGroup.add(rdbtnSPCRadioButton_3);
		rdbtnSPCRadioButton_3.setBounds(244, 181, 140, 23);
		add(rdbtnSPCRadioButton_3);
		setVisible(true);

		buttList[0] = rdbtnAuthorRadioButton;
		buttList[1] = rdbtnReviewerRadioButton_1;
		buttList[2] = rdbtnPCRadioButton_2;
		buttList[3] = rdbtnSPCRadioButton_3;
		
		btnLogButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				if (loginFlag == 0) {
					//check user name, if it exists, load conferences and unlock radio buttons
					removeConfs();
					exists = false;
					me = null;
					clearSelected();
					for (int i = 0; i < users.size(); i++) {
						if (textField.getText().equals(users.get(i).getName())) {
							me = users.get(i);
							exists = true;
							comboBox.setEnabled(true);
							buttList[0].setEnabled(true);
							buttList[0].setSelected(true);
							btnLogButton.setText("Confirm");
							loginFlag = 1;
							comboBox.setSelectedIndex(0);
							textField.setEnabled(false);
							break;
						}
					}
					if (!exists) { //if the user name does not exist, prompt to enter correct name.
						JOptionPane.showMessageDialog(null, "Please enter a valid user name.");
					}
				} else {
					if (buttList[0].isSelected()) 
						mf.launchMain(0, confs.get(comboBox.getSelectedIndex()), me);
					else if (buttList[1].isSelected()) 
						mf.launchMain(1, confs.get(comboBox.getSelectedIndex()), me);
					else if (buttList[2].isSelected())
						mf.launchMain(2, confs.get(comboBox.getSelectedIndex()), me);
					else if (buttList[3].isSelected())
						mf.launchMain(3, confs.get(comboBox.getSelectedIndex()), me);
					
					//mf.launchMain(accessLevel, confs.get(comboBox.getSelectedIndex()), me);
				}
			}
		});

	}

	/** Removes the conference. */
	private void removeConfs() {
		if (comboBox.isEnabled()) {
			comboBox.setEnabled(false);
		}
	}

	/** Clears the selected buttons. */
	private void clearSelected() {
		accessLevel = 0;
		for (int i = 0; i < buttList.length; i++) {
			buttList[i].setSelected(false);
			buttList[i].setEnabled(false);
		}
	}

	/** Loads the users. */
	private void loadUsers() {
		users = dbase.getUsers();
	}

	/** Loads the conference. */
	private void loadConf() { 
		confs = dbase.getConferences();
	}

	/** Sets up the conference. */
	private void setupConfs() {
		for (int i = 0; i < confs.size(); i++) {
			comboBox.addItem(confs.get(i));
		}
		comboBox.setEnabled(false);
	}
}
